Given a positive
integer n, determine whether it is divisible by both a and b
simultaneously.
Input. Three positive
integers n, a, and b, each not exceeding 109.
Output. Print “YES” if n
is divisible by both a and b simultaneously. Otherwise, print “NO”.
Sample input 1 |
Sample output 1 |
12 4 6 |
YES |
|
|
Sample input 2 |
Sample output 2 |
10 5 6 |
NO |
conditional
statement
Algorithm
analysis
Using a
conditional operator, check whether n is divisible by both a and b
simultaneously.
Algorithm implementation
Read the input data.
scanf("%d %d %d", &n, &a, &b);
Check whether n is divisible by both a and
b simultaneously. Depending
on the result, print the
corresponding answer.
if (n % a == 0 && n % b == 0)
printf("YES\n");
else
printf("NO\n");
Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int a = con.nextInt();
int b = con.nextInt();
if (n % a == 0 &&
n % b == 0)
System.out.println("YES");
else
System.out.println("NO");
con.close();
}
}
Python implementation
Read the input data.
n, a, b = map(int,input().split())
Check whether n is divisible by both a and
b simultaneously. Depending
on the result, print the
corresponding answer.
if n % a == 0 and n % b == 0:
print("YES")
else:
print("NO")